<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Welcome to Design Zone......</title>
        <link rel="stylesheet" type="text/css" href="scripts/ext/resources/css/ext-all.css" />
        <link rel="stylesheet" type="text/css" href="styles/dzbase.css" />
        <script type="text/javascript" src="scripts/ext/adapter/ext-base.js">
        </script>
        <script type="text/javascript" src="scripts/ext/ext-all.js">
        </script>
        <script type="text/javascript" src="scripts/utils.js">
        </script>
        <script type="text/javascript">
            Ext.onReady(function(){
                Ext.QuickTips.init();
                
                // turn on validation errors beside the field globally
                Ext.form.Field.prototype.msgTarget = 'side';
                
                var idValue = getPrarmeterValue(window.location, 'id');
                var startPage = getPrarmeterValue(window.location, 'startPage');
                var yarnForm = new Ext.FormPanel({
                    labelAlign: 'top',
                    labelWidth: 75, // label settings here cascade unless overridden
                    url: 'YarnServlet?action=1',
                    frame: true,
                    reader: new Ext.data.XmlReader({
                        record: 'item'
                    }, [{
                        name: 'id',
                        mapping: 'id'
                    }, {
                        name: 'name',
                        mapping: 'name'
                    },{
                        name: 'price',
                        mapping: 'price'
                    },{
                        name: 'weight1',
                        mapping: 'weight1'
                    },{
                        name: 'coefficient1',
                        mapping: 'coefficient1'
                    },{
                        name: 'weight2',
                        mapping: 'weight2'
                    },{
                        name: 'coefficient2',
                        mapping: 'coefficient2'
                    },{
                        name: 'weight3',
                        mapping: 'weight3'
                    },{
                        name: 'coefficient3',
                        mapping: 'coefficient3'
                    },{
                        name: 'comment',
                        mapping: 'comment'
                    }]),
                    title: 'Edit yarn',
                    bodyStyle: 'padding:5px 5px 5px 5px',
                    defaults: {
                        width: '90%'
                    },
                    defaultType: 'textfield',
                    
                    items: [{
                        fieldLabel: 'ID',
                        id: 'yarnId',
                        name: 'id',
                        xtype: 'hidden'
                    }, {
                        fieldLabel: 'Yarn name',
                        name: 'name',
                        allowBlank: false,
                        blankText: 'This fieled must be filled out.',
                        maxLength: 64,
                        maxLengthText: 'The length of the text is too long to store.'
                    }, {
                        fieldLabel: 'Comment',
                        name: 'comment',
                        allowBlank: true,
                        xtype: 'textarea',
                        maxLength: 512,
                        maxLengthText: 'The length of the text is too long to store.'
                    }],
                    
                    buttons: [{
                        text: 'Save',
                        handler: function(){
                            if (yarnForm.getForm().isValid()) {
                                //yarnForm.hide();
                                yarnForm.getForm().submit({
                                    method: 'post',
                                    success: function(form, action){
                                        //alert(form);
                                        messageTip.msg('Tip', 'Save successfully.');
                                        yarnForm.getForm().reset();
                                        //yarnForm.show();
                                    },
                                    failure: function(form, action){
                                        //alert(form);
                                        messageTip.msg('Tip', 'Save unsuccessfully.');
                                        //yarnForm.show();
                                    }
                                });
                            }
                        }
                    },{
						text: 'Reset',
						handler: function(){
							yarnForm.getForm().reset();
						}
					}, {
                        text: 'Cancel',
                        handler: function(){
                            if (startPage != null) {
                                window.location = 'yarn_list.html?startPage=' + startPage;
                            }
                            else {
                                window.location = 'yarn_list.html';
                            }
                        }
                    }]
                });
				
				yarnForm.render('yarnDiv');
	
		
				if(idValue != null){
					document.getElementById('yarnId').value = idValue;
					yarnForm.getForm().load({url:'YarnServlet?action=3&id=' + idValue, waitMsg:'Loading...'});
				}
            });
        </script>
    </head>
    <body>
        <div id="yarnDiv"></div>
    </body>
</html>
